import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import './pages/index_page.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import './pages/login/login_page.dart';
import './pages/login/register_page.dart';
import './pages/jiayouzhan/jyz_list_page.dart';
import './tool/translations.dart';
import './pages/jiayouzhan/jyz_detail_page.dart';
import './pages/jiayouzhan/jyz_pay_page.dart';
import './pages/shop/shop_detail_page.dart';
import './pages/shop/pdd_page.dart';
import './pages/shop/pdd_detail_page.dart';
import './pages/shop/tb_cate_page.dart';
import './pages/user/set_page.dart';
import './pages/login/forget_page.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:flutter/services.dart';
import 'dart:io';
import 'dart:async';
import 'package:flutter_localizations/flutter_localizations.dart';
import './tool/ChineseCupertinoLocalizations.dart';
import './tool/application.dart';
import 'package:flutter_alibc/flutter_alibc.dart';
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;

void main() async {
  flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
  // WidgetsFlutterBinding.ensureInitialized();//起码要放在高德地图初始化方法的前面
  // AMapLocationPurpose
//  AMapLocationClient.setApiKey("cd5a91b2e1dad5541274eb2738cc2da6");
//  AMap.init('cd5a91b2e1dad5541274eb2738cc2da6');
  
  runZoned(() {
    runApp(MyApp());
  }, onError: (Object error, StackTrace stack) {
    print(error);
    print(stack);
  });
var result = await FlutterAlibc.initAlibc(version:"1.0.0",appName:"油然而省");
  if (Platform.isAndroid) {
    SystemUiOverlayStyle systemUiOverlayStyle =
        const SystemUiOverlayStyle(statusBarColor: Colors.transparent);
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  }
}

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new MyAppState();
  }
}

class MyAppState extends State<MyApp> {
  SpecificLocalizationDelegate _localeOverrideDelegate;

  @override
  void initState() {
    super.initState();
    var initializationSettingsAndroid =
        AndroidInitializationSettings('ic_launcher');
    var initializationSettingsIOS = IOSInitializationSettings();
    var initializationSettings = InitializationSettings(
        initializationSettingsAndroid, initializationSettingsIOS);
    flutterLocalNotificationsPlugin.initialize(initializationSettings,
        onSelectNotification: onSelectNotification);

    /// 初始化一个新的Localization Delegate，有了它，当用户选择一种新的工作语言时，可以强制初始化一个新的Translations
    _localeOverrideDelegate = new SpecificLocalizationDelegate(null);

    /// 保存这个方法的指针，当用户改变语言时，我们可以调用applic.onLocaleChanged(new Locale('en',''));，通过SetState()我们可以强制App整个刷新
    applic.onLocaleChanged = onLocaleChange;
  }

  /// 改变语言时的应用刷新核心，每次选择一种新的语言时，都会创造一个新的SpecificLocalizationDelegate实例，强制Translations类刷新。
  onLocaleChange(Locale locale) {
    setState(() {
      _localeOverrideDelegate = new SpecificLocalizationDelegate(locale);
    });
  }

  Future onSelectNotification(String payload) async {
    if (payload != null) {
      debugPrint('notification payload: ' + payload);
    }
//payload 可作为通知的一个标记，区分点击的通知。
    if (payload != null && payload == "complete") {
      print('object');
      // await Navigator.push(
      //   context,
      //   new MaterialPageRoute(builder: (context) => new SecondScreen(payload)),
      // );
    }
  }

  @override
  Widget build(BuildContext context) {
    // ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
    return Container(
      child: MaterialApp(
        title: '油然而省',
        debugShowCheckedModeBanner: false,
        theme: ThemeData(primaryColor: Color.fromRGBO(72, 116, 213, 1)),
        home: IndexPage(),
      //   theme: new ThemeData(
      //   platform: TargetPlatform.iOS,
      //   // fontFamily: "micro", // 统一指定应用的字体。
      // ),
        routes: <String, WidgetBuilder>{
          'login': (_) => LoginPage(),
          'register': (_) => RegisterPage(),
          'jyzList': (_) => JYZListPage(),
          'jyzDetail': (_) => JYZDetailPage(),
          'jyzPay': (_) => JYZPayPage(),
          'shopDetail': (_) => ShopDetailPage(),
          'pdd': (_) => PddPage(),
          'pddDetail': (_) => PddShopDetailPage(),
          'tbCate': (_) => TBCategoryPage(),
          'set': (_) => SetPage(),
          'forget': (_) => ForgetPage(),
        },
        localizationsDelegates: <LocalizationsDelegate<dynamic>>[
          _localeOverrideDelegate,
          ChineseCupertinoLocalizations.delegate, // 自定义的delegate
          DefaultCupertinoLocalizations.delegate, // 目前只包含英文
          const TranslationsDelegate(),
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          GlobalEasyRefreshLocalizations.delegate,
        ],
        supportedLocales: [
          Locale('en', ''),
          Locale('zh', 'CN'),
          const Locale('en', 'US'), // English
          const Locale('zh', ''), // China

          // ... other locales the app supports
        ],
      ),
    );
  }
}
